﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitKernel.RevitObjectSerializeUtility;

namespace YJKRevitArea.AreaThumbnail
{
	[Serializable]
	public partial class FormFirePreventionThumbnail : System.Windows.Forms.Form
	{
		public static string ScalingRatio
		{
			get
			{
				return FormFirePreventionThumbnail.m_strScalingRatio;
			}
			set
			{
				FormFirePreventionThumbnail.m_strScalingRatio = value;
			}
		}

		public FormFirePreventionThumbnail(Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
			this.InitializeComponent();
			string text = Product.UserDataLocation + "\\FirePreventionThumbnailFormData.xml";
			if (File.Exists(text))
			{
				Exception ex;
				new HYRevitObjectSerializeManger().TryGetAPersistentClassByXML<FirePreventionThumbnailData>(text, out this.m_FirePreventionThumbnailDataSave, out ex, true);
				if (this.m_FirePreventionThumbnailDataSave == null)
				{
					this.m_FirePreventionThumbnailDataSave = new FirePreventionThumbnailData();
				}
				this.chkFill.Checked = this.m_FirePreventionThumbnailDataSave.IfSelectFill;
				this.chkGrid.Checked = this.m_FirePreventionThumbnailDataSave.IfSelectGird;
			}
			this.InitFormLineType();
			this.InitFormScalingRatio();
			this.InitFormView();
			FormFirePreventionThumbnail.ScalingRatio = this.cmbScalingRatio.Text;
			this.dgvViewPlan.Columns[this.dgvViewPlan.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		}

		public FirePreventionThumbnailData GetFirepPreventionThumbnailData()
		{
			this.m_FirePreventionThumbnailData.IfSelectFill = this.chkFill.Checked;
			this.m_FirePreventionThumbnailData.IfSelectGird = this.chkGrid.Checked;
			this.m_FirePreventionThumbnailData.ScalingRatio = this.GetScalingRatio(this.cmbScalingRatio.Text);
			if (this.m_lstLinePatternElement.Find((GraphicsStyle n) => n.Name.Equals(this.cmbLineType.Text)) != null)
			{
				this.m_FirePreventionThumbnailData.LineType = this.m_lstLinePatternElement.Find((GraphicsStyle n) => n.Name.Equals(this.cmbLineType.Text)).Name;
			}
			if (this.m_lstViewPlan.Count != 0 && this.m_lstViewPlan.Count == this.dgvViewPlan.RowCount)
			{
				this.m_FirePreventionThumbnailData.ViewId = -1;
				if (this.dgvViewPlan.CurrentCell.ColumnIndex == 0)
				{
					int rowIndex = this.dgvViewPlan.CurrentCell.RowIndex;
					this.m_FirePreventionThumbnailData.ViewId = this.m_lstViewPlan[rowIndex].Id.IntegerValue;
				}
			}
			this.m_FirePreventionThumbnailData.LstScalingRatio.Clear();
			foreach (object obj in this.cmbScalingRatio.Items)
			{
				this.m_FirePreventionThumbnailData.LstScalingRatio.Add(obj.ToString());
			}
			return this.m_FirePreventionThumbnailData;
		}

		public int GetSetUpClick()
		{
			return this.m_nClick;
		}

		private double GetScalingRatio(string strScalingRatio)
		{
			try
			{
				string[] array = strScalingRatio.Split(new char[]
				{
					':'
				});
				if (array.Length == 1)
				{
					Array.Clear(array, 0, array.Length);
					array = strScalingRatio.Split(new char[]
					{
						'：'
					});
				}
				if (array.Length == 2)
				{
					return Convert.ToDouble(array[0]) / Convert.ToDouble(array[1]);
				}
			}
			catch
			{
			}
			return 1.0;
		}

		private void InitFormLineType()
		{
			foreach (GraphicsStyle graphicsStyle in FirePreventionThumbnailManager.GetAllLineType(this.m_doc))
			{
				this.cmbLineType.Items.Add(graphicsStyle.Name);
			}
			if (this.cmbLineType.Items.Count > 0)
			{
				if (this.m_FirePreventionThumbnailDataSave.LineType != null && this.m_FirePreventionThumbnailDataSave.LineType != "")
				{
					if (this.cmbLineType.Items.Contains(this.m_FirePreventionThumbnailDataSave.LineType))
					{
						this.cmbLineType.Text = this.m_FirePreventionThumbnailDataSave.LineType;
					}
				}
				else
				{
					this.cmbLineType.SelectedIndex = 0;
				}
			}
			this.m_lstLinePatternElement = FirePreventionThumbnailManager.GetAllLineType(this.m_doc);
		}

		private void InitFormScalingRatio()
		{
			double scalingRatio = this.m_FirePreventionThumbnailDataSave.ScalingRatio;
			string text = "1:" + (1.0 / scalingRatio).ToString();
			List<string> lstScalingRatio = this.m_FirePreventionThumbnailDataSave.LstScalingRatio;
			if (lstScalingRatio.Count <= 0)
			{
				this.cmbScalingRatio.Items.Add("1:1");
				this.cmbScalingRatio.Items.Add("1:2");
				this.cmbScalingRatio.Items.Add("1:5");
				this.cmbScalingRatio.Items.Add("1:10");
				this.cmbScalingRatio.Items.Add("1:20");
				this.cmbScalingRatio.Items.Add("1:50");
				this.cmbScalingRatio.Items.Add("1:100");
				this.cmbScalingRatio.SelectedIndex = 0;
				return;
			}
			foreach (string item in lstScalingRatio)
			{
				this.cmbScalingRatio.Items.Add(item);
			}
			if (this.cmbScalingRatio.Items.Contains(text))
			{
				this.cmbScalingRatio.Text = text;
				return;
			}
			this.cmbScalingRatio.SelectedIndex = 0;
		}

		private void InitFormView()
		{
			this.m_lstViewPlan = FirePreventionThumbnailManager.GetAllView(this.m_doc);
			if (this.m_lstViewPlan.Count < 1)
			{
				return;
			}
			for (int i = 0; i < this.m_lstViewPlan.Count; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = false.ToString();
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = (i + 1).ToString();
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = this.m_lstViewPlan[i].Title.ToString();
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				this.dgvViewPlan.Rows.Add(dataGridViewRow);
			}
			this.dgvViewPlan.Rows[0].Cells[0].Value = true.ToString();
		}

		private void FormFirePreventionThumbnail_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void dgvViewPlan_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (this.dgvViewPlan.CurrentCell.ColumnIndex == 0)
				{
					Thread.Sleep(222);
					int rowIndex = this.dgvViewPlan.CurrentCell.RowIndex;
					DataGridViewCheckBoxCell dataGridViewCheckBoxCell = (DataGridViewCheckBoxCell)this.dgvViewPlan.CurrentCell;
					bool flag = Convert.ToBoolean(dataGridViewCheckBoxCell.EditedFormattedValue);
					if (flag)
					{
						for (int i = 0; i < this.m_lstViewPlan.Count; i++)
						{
							if (rowIndex != i)
							{
								this.dgvViewPlan.Rows[i].Cells[0].Value = false.ToString();
							}
						}
						this.m_strViewName = this.dgvViewPlan.Rows[rowIndex].Cells[2].Value.ToString();
					}
					else
					{
						int num = 0;
						for (int j = 0; j < this.m_lstViewPlan.Count; j++)
						{
							if (this.dgvViewPlan.Rows[j].Cells[0].Value.Equals("true") || this.dgvViewPlan.Rows[j].Cells[0].Value.Equals("True"))
							{
								num++;
							}
						}
						if (num <= 1)
						{
							dataGridViewCheckBoxCell.EditingCellFormattedValue = !flag;
							dataGridViewCheckBoxCell.Value = !flag;
							this.m_strViewName = this.dgvViewPlan.Rows[rowIndex].Cells[2].Value.ToString();
							return;
						}
					}
				}
				this.Refresh();
			}
			catch
			{
			}
		}

		private void btnSetUp_Click(object sender, EventArgs e)
		{
			FormFirePreventionThumbnailSetUp formFirePreventionThumbnailSetUp = new FormFirePreventionThumbnailSetUp();
			if (this.m_nClick == 1)
			{
				formFirePreventionThumbnailSetUp.txtCharacterHeight.Text = this.m_FirePreventionThumbnailData.FirePreventionThumbnailSetUpData.CharacterHeight.ToString();
				formFirePreventionThumbnailSetUp.txtRatioOfHeightToWidth.Text = this.m_FirePreventionThumbnailData.FirePreventionThumbnailSetUpData.RatioOfHeightToWidth.ToString();
				formFirePreventionThumbnailSetUp.cmbTypeface.Text = this.m_FirePreventionThumbnailData.FirePreventionThumbnailSetUpData.TypefaceName.ToString();
			}
			if (formFirePreventionThumbnailSetUp.ShowDialog() == DialogResult.OK)
			{
				this.m_nClick = 1;
				this.m_FirePreventionThumbnailData.FirePreventionThumbnailSetUpData = formFirePreventionThumbnailSetUp.SetFirePreventionThumbnailSetUpData();
				return;
			}
			this.m_nClick = 0;
			formFirePreventionThumbnailSetUp.Close();
		}

		private void cmbScalingRatio_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == ':' || e.KeyChar == '：' || e.KeyChar == '\b')
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		private void cmbScalingRatio_MouseLeave(object sender, EventArgs e)
		{
			int num = 0;
			string text = this.cmbScalingRatio.Text;
			for (int i = 0; i < text.Length; i++)
			{
				if (text[i].Equals(':'))
				{
					num++;
				}
			}
			string[] array = this.cmbScalingRatio.Text.Split(new char[]
			{
				':'
			});
			List<string> list = new List<string>();
			foreach (string text2 in array)
			{
				if (text2 != "")
				{
					list.Add(text2);
				}
			}
			if (num != 1 || list.Count == 1)
			{
				YJKMessageBox.Show("视图比例输入有误，请重新输入，输入格式为“数字 + : + 数字”，例如:“1:100”,注意“:”是英文输入");
				this.cmbScalingRatio.Text = FormFirePreventionThumbnail.ScalingRatio;
				return;
			}
			FormFirePreventionThumbnail.ScalingRatio = this.cmbScalingRatio.Text;
			if (this.cmbScalingRatio.Items.Contains(this.cmbScalingRatio.Text))
			{
				return;
			}
			if (this.cmbScalingRatio.Items.Count > 8)
			{
				this.cmbScalingRatio.Items.RemoveAt(this.cmbScalingRatio.Items.Count - 1);
			}
			this.cmbScalingRatio.Items.Insert(0, this.cmbScalingRatio.Text);
		}

		private void FormFirePreventionThumbnail_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private int m_nClick;

		private Document m_doc;

		private string m_strViewName = string.Empty;

		private FirePreventionThumbnailData m_FirePreventionThumbnailData = new FirePreventionThumbnailData();

		private List<GraphicsStyle> m_lstLinePatternElement = new List<GraphicsStyle>();

		private List<Autodesk.Revit.DB.View> m_lstViewPlan = new List<Autodesk.Revit.DB.View>();

		private FirePreventionThumbnailData m_FirePreventionThumbnailDataSave = new FirePreventionThumbnailData();

		private static string m_strScalingRatio = string.Empty;
	}
}
